package com.white.androidstudy;

import android.content.Context;
import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.white.androidstudy.bean.ComplexBean;
import com.white.androidstudy.bean.ComplexChild;
import com.white.commonlib.adapter.recycle.BaseLoadMoreViewHolder;
import com.white.commonlib.adapter.recycle.BaseViewHolder;
import com.white.commonlib.adapter.recycle.GridStaggerDivider;
import com.white.commonlib.adapter.recycle.RecycleBaseAdapter;
import com.white.commonlib.engine.DefaultRecycleViewSetter;
import com.white.commonlib.engine.img.DefaultImageLoader;
import com.white.commonlib.engine.img.IImageLoader;

import java.util.ArrayList;
import java.util.List;

public class RecycleEmbededActivity extends AppCompatActivity
{

    RecyclerView recycle;

    List<ComplexBean> datas;
    RecyclerView.RecycledViewPool viewPool;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_embeded);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        init();
    }

    private void init()
    {
        datas = new ArrayList<>();
        List<ComplexChild> cs = new ArrayList<>();
        ComplexChild cc = new ComplexChild();
        cc.id = 0;
        cc.des = "water lop";
        cc.img = "https://cdn.pixabay.com/photo/2017/11/12/19/22/high-speed-2943518__340.jpg";
        cs.add(cc);
        cc = new ComplexChild();
        cc.id = 1;
        cc.des = "beauty1 lop";
        cc.img = "https://cdn.pixabay.com/photo/2017/11/10/12/53/face-2936245__340.jpg";
        cs.add(cc);
        cc = new ComplexChild();
        cc.id = 2;
        cc.des = "beauty2 lop";
        cc.img = "https://cdn.pixabay.com/photo/2017/11/11/21/55/girl-2940655__340.jpg";
        cs.add(cc);
        cc = new ComplexChild();
        cc.id = 3;
        cc.des = "oldman lop";
        cc.img = "https://cdn.pixabay.com/photo/2017/09/25/13/12/man-2785071__340.jpg";
        cs.add(cc);
        cc = new ComplexChild();
        cc.id = 4;
        cc.des = "lake lop";
        cc.img = "https://cdn.pixabay.com/photo/2017/09/18/22/20/lake-2763432__340.jpg";
        cs.add(cc);

        for (int i = 0; i < 100; i++)
        {
            ComplexBean cb = new ComplexBean();
            cb.id = i;
            cb.des = "new Destrioadifa";
            cb.childs = cs;
            datas.add(cb);
        }

        AdapterParent a = new AdapterParent(this, datas);
        recycle.setRecycledViewPool(viewPool);
        recycle.addOnScrollListener(new RecyclerView.OnScrollListener()
        {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState)
            {
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE: // The RecyclerView is not currently scrolling.
                        //对于滚动不加载图片的尝试
                        Glide.with(RecycleEmbededActivity.this).resumeRequests();
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING: // The RecyclerView is currently being dragged by outside input such as user touch input.
                        Glide.with(RecycleEmbededActivity.this).pauseRequests();
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING: // The RecyclerView is currently animating to a final position while not under
                        Glide.with(RecycleEmbededActivity.this).pauseRequests();
                        break;
                }
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
        new DefaultRecycleViewSetter(this,recycle, a)
                .set();
    }

    class AdapterParent extends RecycleBaseAdapter<ComplexBean>
    {

        public AdapterParent(Context context, List<ComplexBean> data)
        {
            super(context, data);
        }

        @Override
        public boolean needLoadMoreSupport()
        {
            return false;
        }

        @Override
        public RecyclerView.ViewHolder getCustomHolder(Context context, ViewGroup parent, int viewType, View v)
        {
            return new VH(context, v);
        }

        @Override
        public int getCustomView(int viewType)
        {
            return R.layout.item_complex_parent;
        }

        @Override
        public BaseLoadMoreViewHolder getMoreHolder(ViewGroup parent, View v)
        {
            return null;
        }

        @Override
        public int getLoadMoreView()
        {
            return 0;
        }

        class VH extends BaseViewHolder<ComplexBean>
        {
            TextView tv;
            RecyclerView child_cycle;
            GridLayoutManager m ;
            GridStaggerDivider d;
            public VH(Context context, View v)
            {
                super(context, v);
                tv = (TextView) v.findViewById(R.id.des);
                child_cycle = (RecyclerView) v.findViewById(R.id.child_cycle);
                child_cycle.setRecycledViewPool(viewPool);
                m = new GridLayoutManager(mContext, 4);
            }

            @Override
            public void setContent(ComplexBean data, int position)
            {
                tv.setText(data.des);
                if(d != null)
                    child_cycle.removeItemDecoration(d);
                AdapterChild ad = new AdapterChild(mContext, data.childs);
                child_cycle.setLayoutManager(m);
                child_cycle.setAdapter(ad);
                d = new GridStaggerDivider(mContext);
                d.setDividerStyle(30, Color.TRANSPARENT);
                child_cycle.addItemDecoration(d);
            }
        }
    }

    class AdapterChild extends RecycleBaseAdapter<ComplexChild>
    {
        RecyclerView parent;
        int parentWidth = 1040;
        int dividerWidth;
        int desireWidth;
        int spanCount;

        public AdapterChild(Context context, List<ComplexChild> data)
        {
            super(context, data);
            dividerWidth = 30;
        }

        @Override
        public void onAttachedToRecyclerView(final RecyclerView rv)
        {
            super.onAttachedToRecyclerView(rv);
            RecyclerView.LayoutManager mm = rv.getLayoutManager();
            if(mm instanceof GridLayoutManager)
            {
                spanCount = ((GridLayoutManager)mm).getSpanCount();
            }
        }

        @Override
        public boolean needLoadMoreSupport()
        {
            return false;
        }

        @Override
        public RecyclerView.ViewHolder getCustomHolder(Context context, ViewGroup parent, int viewType, View v)
        {
            return new VH(context, v);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            super.onBindViewHolder(holder, position);
            VH hh = (VH) holder;
            ConstraintLayout.LayoutParams p = (ConstraintLayout.LayoutParams) hh.iv.getLayoutParams();


            desireWidth = (parentWidth - (spanCount-1) * dividerWidth) / spanCount;

            p.width = desireWidth;
            p.height = desireWidth;
            hh.iv.setLayoutParams(p);
        }

        @Override
        public int getCustomView(int viewType)
        {
            return R.layout.item_complex_child;
        }

        @Override
        public BaseLoadMoreViewHolder getMoreHolder(ViewGroup parent, View v)
        {
            return null;
        }

        @Override
        public int getLoadMoreView()
        {
            return 0;
        }

        class VH extends BaseViewHolder<ComplexChild>
        {
            TextView tv;
            ImageView iv;
            ConstraintLayout parent;

            DefaultImageLoader loader;
            public VH(Context context, View v)
            {
                super(context, v);
                tv = (TextView) v.findViewById(R.id.des);
                iv = (ImageView) v.findViewById(R.id.photo);
                parent = (ConstraintLayout) v.findViewById(R.id.parent);
                loader = new DefaultImageLoader(mContext);
            }

            @Override
            public void setContent(ComplexChild data, int position)
            {
                tv.setText(data.des);
//                loader.showImg(data.img, iv);
                Glide.with(mContext)
                        .load(data.img)
                        .into(new GlideDrawableImageViewTarget(iv)
                        {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation)
                            {
                                super.onResourceReady(resource, animation);
                            }
                        });
            }
        }
    }
}
